import { FilterConst } from "../../../component/gconst/FilterConst";
import { SceneConfig } from "../../resource/SceneConfig";
import { Alert } from "../../../../com/mui/controls/Alert";
import { Language } from "../../../../extend/language/Language";
import { LoaderManager } from "../../../../com/gengine/resource/LoaderManager";
import { MapFileUtil } from "../../scene/map/MapFileUtil";
import { SWFInfo } from "../../../../com/gengine/resource/info/SWFInfo";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { GButton } from "../../../../com/mui/controls/GButton";
import { ItemLayer } from "./ItemLayer";
import { Cache } from "../../cache/Cache";
import { GameMapUtil } from "../../scene/map/GameMapUtil";
import { TaskTargetInfo } from "../../model/TaskTargetInfo";
import { EntityType } from "../../rules/EntityType";
import { AIManager } from "../../scene/ai/AIManager";
import { FlyFootManager } from "../../manager/FlyFootManager";
import { LoaderPriority } from "../../../../com/gengine/resource/LoaderPriority";
import { SceneInfo } from "../../scene/map/SceneInfo";
type int = number;
//class WorldMap
    
    export  class WorldMap /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._mouseOverFilter = [FilterConst.worldMapFilter2, FilterConst.worldMapFilter];
            this._mouseOutFilter = [FilterConst.worldMapFilter2];
            this.initUI();
            return;
        }

        public showCityMapById(arg1: int): void
        {
            this._mapId = arg1;
            this._scene = SceneConfig.instance.getSceneInfo(this._mapId);
            if (this._scene == null) 
            {
                Alert.show(Language.getString(20637));
            }
            else 
            {
                LoaderManager.instance.load(MapFileUtil.getMiniMapPathByMap(this._mapId), this.onCityMapLoaded);
            }
            return;
        }

        /* internal  */onMouseOverHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as Object /* flash.display.DisplayObject */;
            if (loc1 != null) 
            {
                if (loc1 != this._lastMC) 
                {
                    this._lastMC = loc1;
                    this._lastMC.filters = this._mouseOverFilter;
                }
            }
            return;
        }

        /* internal  */onMouseOutHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as Object /* flash.display.DisplayObject */;
            if (loc1 != null) 
            {
                if (loc1 == this._lastMC) 
                {
                    this._lastMC.filters = this._mouseOutFilter;
                    this._lastMC = null;
                }
            }
            return;
        }

        /* internal  */onMapLoaded(arg1: SWFInfo): void
        {
            this._mapMC = arg1.clip;
            this._mapMC.x = 0;
            this._mapMC.y = 0;
            this._mapMC.addEventListener(flash.events.MouseEvent.CLICK, this.onMouseClickHandler);
            this._mapMC.addEventListener(flash.events.MouseEvent.MOUSE_OVER, this.onMouseOverHandler);
            this._mapMC.addEventListener(flash.events.MouseEvent.MOUSE_OUT, this.onMouseOutHandler);
            if (!this._goBackBtn || !this._goBackBtn.parent) 
            {
                addChild(this._mapMC);
            }
            return;
        }

        /* internal  */onCityMapLoaded(arg1: ImageInfo): void
        {
            if (!arg1 || !arg1.bitmapData) 
            {
                return;
            }
            this._cityMap.bitmapData = arg1.bitmapData;
            var loc1=(this.W - 0) / this._cityMap.width;
            var loc2=(this.H - 0) / this._cityMap.height;
            var loc3=Math.min(loc1, loc2);
            this._cityMap.width = this._cityMap.width * loc3;
            this._cityMap.height = this._cityMap.height * loc3;
            if (this._scene != null) 
            {
                this._scaleWidth = this._cityMap.width / this._scene.sMapDefine.realWidth;
                this._scaleHeight = this._cityMap.height / this._scene.sMapDefine.realHeight;
            }
            this._cityMap.x = (this.W - this._cityMap.width) / 2 - 0;
            this._cityMap.y = (this.H - this._cityMap.height) / 2 - 0;
            if (this._mapMC && contains(this._mapMC)) 
            {
                removeChild(this._mapMC);
            }
            addChild(this._citySprite);
            if (!this._goBackBtn) 
            {
                this._goBackBtn = new GButton();
                this._goBackBtn.label = Language.getString(20638);
                this._goBackBtn.styleName = "Button";
                this._goBackBtn.textField.filters = [FilterConst.nameGlowFilter];
                this._goBackBtn.setSize(75, 26);
                this._goBackBtn.x = this.W - this._goBackBtn.width - 20;
                this._goBackBtn.y = this.H - this._goBackBtn.height - 10;
                this._goBackBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onGoBackClickHandler);
            }
            addChild(this._goBackBtn);
            this._cityBg.width = this.W - 0;
            this._cityBg.height = this.H - 0;
            if (!this._itemLayer) 
            {
                this._itemLayer = new ItemLayer(false);
            }
            this._itemLayer.x = this._cityMap.x;
            this._itemLayer.y = this._cityMap.y;
            this._citySprite.addChild(this._itemLayer);
            this._itemLayer.updateSceneUnit(this._scaleWidth, this._scaleHeight, this._scene, false);
            if (Cache.instance.huntTask.isShowLocationTarget(this._mapId)) 
            {
                this._itemLayer.targetLocation(this._mapId, Cache.instance.huntTask.huntTaskInfo.px, Cache.instance.huntTask.huntTaskInfo.py, this._scaleWidth, this._scaleHeight);
            }
            return;
        }

        /* internal  */onPicBgLoad(arg1: ImageInfo): void
        {
            if (arg1 && arg1.bitmapData) 
            {
                this._cityBg.bitmapData = arg1.bitmapData;
                this._cityBg.width = this.W - 0;
                this._cityBg.height = this.H - 0;
            }
            return;
        }

        /* internal  */onMapLoadPro(arg1: Object /* flash.events.ProgressEvent */): void
        {
            return;
        }

        /* internal  */onMapLoadFail(arg1): void
        {
            return;
        }

        /* internal  */onCityMapClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (arg1.target is flash.display.Sprite) 
            {
                if ((arg1.target as Object /* flash.display.Sprite */).name == "citySprite") 
                {
                    this._tilePoint = GameMapUtil.localToTilePoint(arg1.localX - this._cityMap.x - 2, arg1.localY - this._cityMap.y - 2, 1 / this._scaleWidth, 1 / this._scaleHeight);
                    if (!this._gotoCityTarget) 
                    {
                        this._gotoCityTarget = new TaskTargetInfo();
                    }
                    this._gotoCityTarget.mapId = this._mapId;
                    this._gotoCityTarget.mapName = this._scene.sMapDefine.name;
                    this._gotoCityTarget.x = this._tilePoint.x;
                    this._gotoCityTarget.y = this._tilePoint.y;
                    this._gotoCityTarget.targetType = EntityType.Point;
                    AIManager.onAutoPathAIControl(this._gotoCityTarget);
                    FlyFootManager.showFlyFoot(this, this._citySprite.x + arg1.localX - 15, this._citySprite.y + arg1.localY - 20, this._tilePoint.x, this._tilePoint.y, this._mapId);
                    this._itemLayer.setTargetPosition(arg1.localX - this._itemLayer.x, arg1.localY - this._itemLayer.y);
                    return;
                }
                if (typeof arg1.target === "itemlayer") 
                {
                    this._tilePoint = GameMapUtil.localToTilePoint(arg1.localX, arg1.localY, 1 / this._scaleWidth, 1 / this._scaleHeight);
                    if (!this._gotoCityTarget) 
                    {
                        this._gotoCityTarget = new TaskTargetInfo();
                    }
                    this._gotoCityTarget.mapId = this._mapId;
                    this._gotoCityTarget.mapName = this._scene.sMapDefine.name;
                    this._gotoCityTarget.x = this._tilePoint.x;
                    this._gotoCityTarget.y = this._tilePoint.y;
                    this._gotoCityTarget.targetType = EntityType.Point;
                    AIManager.onAutoPathAIControl(this._gotoCityTarget);
                    FlyFootManager.showFlyFoot(this._itemLayer, arg1.localX - 15, arg1.localY - 20, this._tilePoint.x, this._tilePoint.y, this._mapId);
                    this._itemLayer.setTargetPosition(arg1.localX, arg1.localY);
                    return;
                }
            }
            this._tilePoint = GameMapUtil.localToTilePoint(this._citySprite.mouseX - this._cityMap.x - 2, this._citySprite.mouseY - this._cityMap.y - 2, 1 / this._scaleWidth, 1 / this._scaleHeight);
            this._itemLayer.setTargetPosition(this._citySprite.mouseX - this._itemLayer.x, this._citySprite.mouseY - this._itemLayer.y);
            return;
        }

        public onGoBackClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._goBackBtn && this._goBackBtn.parent) 
            {
                removeChild(this._goBackBtn);
            }
            if (this._citySprite && this._citySprite.parent) 
            {
                removeChild(this._citySprite);
            }
            if (this._mapMC && !contains(this._mapMC)) 
            {
                addChild(this._mapMC);
            }
            this.clean();
            return;
        }

        public clean(): void
        {
            if (this._itemLayer) 
            {
                if (this._itemLayer.parent) 
                {
                    this._itemLayer.parent.removeChild(this._itemLayer);
                }
                this._itemLayer.clean();
            }
            return;
        }

        public targetLocation(arg1: int, arg2: int, arg3: int): void
        {
            if (this._citySprite && this._citySprite.parent) 
            {
                if (this._mapId == arg1) 
                {
                    this._itemLayer.targetLocation(arg1, arg2, arg3, this._scaleWidth, this._scaleHeight);
                }
            }
            return;
        }

        public removeLocation(): void
        {
            this._itemLayer.removeLocation();
            return;
        }

        public isCityMap(): Boolean
        {
            if (this._citySprite && this._citySprite.parent) 
            {
                return true;
            }
            return false;
        }

        public get mapId(): int
        {
            return this._mapId;
        }

        /* internal  */initUI(): void
        {
            LoaderManager.instance.load("map.swf", this.onMapLoaded, LoaderPriority.LevelD, null, this.onMapLoadPro, this.onMapLoadFail);
            this._citySprite = new Object /* flash.display.Sprite */();
            this._citySprite.name = "citySprite";
            this._cityBg = new Object /* flash.display.Bitmap */();
            this._citySprite.addChild(this._cityBg);
            this._cityMap = new Object /* flash.display.Bitmap */();
            this._citySprite.addChild(this._cityMap);
            this._citySprite.addEventListener(flash.events.MouseEvent.CLICK, this.onCityMapClickHandler);
            LoaderManager.instance.load("smallmapbg.jpg", this.onPicBgLoad);
            return;
        }

        /* internal  */onMouseClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as Object /* flash.display.DisplayObject */;
            if (loc1 != null) 
            {
                this._lastMC = loc1;
                this._mapId = parseInt(this._lastMC.name.split("_")[1]);
                this.showCityMapById(this._mapId);
            }
            return;
        }

        public updateSceneUnit(): void
        {
            if (this._scene && this.isCityMap()) 
            {
                this._itemLayer.updateSceneUnit(this._scaleWidth, this._scaleHeight, this._scene, false);
            }
            return;
        }

        public updateNpc(): void
        {
            if (this._scene && this.isCityMap()) 
            {
                this._itemLayer.cleanNPC();
                this.updateSceneUnit();
            }
            return;
        }

        /* internal */ /* const */W: int=586;

        /* internal */ /* const */H: int=350;

        /* internal */ /* var */_cityBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_mapMC: Object /* flash.display.MovieClip */;

        /* internal */ /* var */_lastMC: Object /* flash.display.DisplayObject */;

        /* internal */ /* var */_cityMap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_goBackBtn: GButton;

        protected /* var */_scaleWidth: number=0;

        protected /* var */_scaleHeight: number=0;

        /* internal */ /* var */_mapId: int;

        /* internal */ /* var */_scene: SceneInfo;

        /* internal */ /* var */_citySprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_itemLayer: ItemLayer;

        /* internal */ /* var */_mouseOverFilter: Array<any>;

        /* internal */ /* var */_mouseOutFilter: Array<any>;

        /* internal */ /* var */_tilePoint: Object /* flash.geom.Point */;

        /* internal */ /* var */_gotoCityTarget: TaskTargetInfo;
    }
